package 多线程.AQS.countDownLathTest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class TestCountDownLatch3 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(5);
        List<String> list=new ArrayList<>();
        Integer lock = 1;
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                //当前计数器的数量
                synchronized (lock) {
                    long count = countDownLatch.getCount();
                    //设置的count为5  但是循环了10次，计数器减少到0后就不会再减少了-->countDownLatch.countDown();
                    countDownLatch.countDown();
                    if(countDownLatch.getCount() != count){
                        list.add("计数器次数变了，此时由值 " + count + "变成了" + countDownLatch.getCount());
                    }
                }

            }).start();
        }
        Thread.sleep(100);
//        System.out.println(list.size());//5
        list.forEach(System.out::println);
        //上述代码，可以看出，每次一个线程运行时，值都会减少，一旦等于0，计数器也不会重置。

    }
}